<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>refs 转发</title>
</head>
<body>
  <div id="root"></div>
  <script src="../js/react.development.js"></script>
  <script src="../js/react-dom.development.js"></script>
  <script src="../js/babel.min.js"></script>
  <script type="text/babel">

    // 函数组件不能够使用 ref，因为函数组件没有实例，可使用 refs 转发来实现
    // function FancyButton(props) {
    //   return (
    //     <button>{ props.children }</button>
    //     // <button {...props}></button>
    //   )
    // }

    // const FancyButton = React.forwardRef(function myFunction(props, ref) {
    const FancyButton = React.forwardRef((props, ref) => {
      console.log('React.forwardRef: ', props, ref)
      return (
        <button ref={ref} onClick={props.onClick}>{ props.children }</button>
      )
    })

    class Demo extends React.Component {

      btnRef = React.createRef()

      handleClick = (e) => {
        console.log(this, this.btnRef.current)
      }

      render() {
        return (
          <React.Fragment>
            <FancyButton ref={this.btnRef} onClick={this.handleClick}>点击获取ref</FancyButton>
            <div>div01</div>
            <div>div02</div>
            <div>div03</div>  
          </React.Fragment>
        )
      }
    }
    ReactDOM.render(<Demo />, document.getElementById('root'))
  </script>
</body>
</html>